package com.organization

/**
 * Created with IntelliJ IDEA.
 * User: inester
 * Date: 12.07.12
 * Time: 16:04
 * To change this template use File | Settings | File Templates.
 */
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException
import org.quartz.JobDataMap
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContextAware
import org.springframework.context.ApplicationContext
import org.codehaus.groovy.grails.web.context.ServletContextHolder
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes;

public class CacheFlushJob implements Job
{
    ApplicationContext ctx =
        ServletContextHolder.getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
    def localDataService = ctx.getBean('localDataService')

    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap = context.getMergedJobDataMap()
        String cacheName = dataMap.getString('cacheName')
        String nodeId = dataMap.getString('nodeId')
        if (cacheName=~"treeCache")
            localDataService.flushTreeCache(nodeId);
        else if(cacheName=~"elementCache")
            localDataService.flushElementCache(nodeId)
    }
}
